package work.yixin.database.test.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import work.yixin.database.test.config.DataSource;
import work.yixin.database.test.config.DataSourceType;
import work.yixin.database.test.dao.mapper.TestAMapper;
import work.yixin.database.test.dao.mapper.TestBMapper;
import work.yixin.database.test.dao.repository.TestARepository;
import work.yixin.database.test.dao.repository.TestBRepository;
import work.yixin.database.test.entity.TestA;
import work.yixin.database.test.entity.TestB;

@RestController
public class IndexController {
	@Autowired
	private TestARepository testARepository;
	@Autowired
	private TestAMapper testAMapper;
	
	@Autowired
	private TestBRepository testBRepository;
	@Autowired
	private TestBMapper testBMapper;
	
	@DataSource(value = DataSourceType.FIRST)
	@GetMapping("/RA")
	@ResponseBody
	public TestA RA() {
		return testARepository.save(new TestA(null, new Date().getTime()+""));
	}
	
	@DataSource(value = DataSourceType.FIRST)
	@GetMapping("/MA")
	@ResponseBody
	public List<TestA> MA() {
	    return testAMapper.findAll();
	}
	
	@DataSource(value = DataSourceType.SECOND)
	@GetMapping("/RB")
	@ResponseBody
	public TestB RB() {
		return testBRepository.save(new TestB(null, new Date().getTime()+""));
	}
	
	@DataSource(value = DataSourceType.SECOND)
	@GetMapping("/MB")
	@ResponseBody
	public List<TestB> MB() {
		 return testBMapper.findAll();
	}

}
